﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Pokemon
{
    public class Blaziken : Pokemon
    {
        private static Card _avatar;
        public Blaziken(Game game, Blank owner, Vector2 position) 
            : base(game, owner, position)
        {
        }

        public static Card Avatar
        {
            get { return _avatar; }
            set { _avatar = value; }
        }

        public override void Initialize()
        {
            XDocument doc = XDocument.Load(@"Content/Textures/Pokemons/Blaziken/BlazikenDef.xml");
            Texture2D texture2 = null;
            Vector2 frameSize = Vector2.Zero;
            Vector2 centerPoint = Vector2.Zero;
            int numFrame= 0;

            XName name = XName.Get(@"Damages");            
            if (doc.Document != null)
            {
                var definitions = doc.Document.Descendants(name);
                _damage = Int32.Parse((definitions.First()).Value);
                
                name = XName.Get("Healths");
                definitions = doc.Document.Descendants(name);
                _health = Int32.Parse((definitions.First()).Value);
                
                name = XName.Get("Speed");
                definitions = doc.Document.Descendants(name);
                _bulledSpeed = Int32.Parse((definitions.First()).Value);

                name = XName.Get("Range");
                definitions = doc.Document.Descendants(name);
                _range = Int32.Parse((definitions.First()).Value);

                name = XName.Get("Delay");
                definitions = doc.Document.Descendants(name);
                _delay = Int32.Parse((definitions.First()).Value);      

                name = XName.Get(@"Definition");
                definitions = doc.Document.Descendants(name);
                foreach (var definition in definitions)
                {
                    var xAttribute = definition.Attribute(@"SheetName");
                    if (xAttribute != null)
                    {
                        texture2 = Game.Content.Load<Texture2D>(xAttribute.Value);                                               
                        xAttribute = definition.Attribute(@"FrameWidth");
                        if (xAttribute != null)
                        {
                            var width = float.Parse(xAttribute.Value);
                            xAttribute = definition.Attribute(@"FrameHeight");
                            if (xAttribute != null)
                            {
                                var height = float.Parse(xAttribute.Value);
                                frameSize = new Vector2(width, height);
                            }
                        }
                    }
                    xAttribute = definition.Attribute(@"XCenterPoint");

                    var xCenterPoint = Int32.Parse(xAttribute.Value);
                    xAttribute = definition.Attribute(@"YCenterPoint");
                    var yCenterPoint = Int32.Parse(xAttribute.Value);
                    centerPoint = new Vector2(xCenterPoint, yCenterPoint);

                    xAttribute = definition.Attribute(@"NumFrame");
                    numFrame = Int32.Parse(xAttribute.Value);

                    xAttribute = definition.Attribute(@"Name");
                    var type = xAttribute.Value;

                    Sprite sprite = new Sprite(texture2, numFrame, frameSize, centerPoint, false);

                    switch (type)
                    {
                        case "Idle":
                            Textures[0] = sprite;
                            break;
                        case "Attack":
                            Textures[1] = sprite;
                            break;
                        case "Hit":
                            Textures[2] = sprite;
                            break;
                        case "Dead":
                            Textures[3] = sprite;
                            break;
                        case "Stand":
                            Textures[4] = sprite;
                            break;
                    } 
                }
            }
            base.Initialize();
        }

        public override void Update(GameTime gameTime)
        {
            _TimeSpan += gameTime.ElapsedGameTime;
            if (_TimeSpan.Milliseconds < 100)
                return;
            base.Update(gameTime);
            _TimeSpan = TimeSpan.Zero;
            int index = GetUnitStateIndex();
            if (_enemy != null)
            {
                if (Blank.GetDistance(GetCenterPoint(), _enemy.GetCenterPoint()) < _range)
                {
                    //Neu nhu dang o trang thai Idle thi chuyen sang Attack
                    if (_UnitState == UnitState.Idle)
                        Attack();
                    //Nguoc lai tuc la dang o trang thai Attack, kiem tra den frame 9 chua, neu thoa thi gay sat thuong len muc tieu
                    else
                        if (Textures[index].FrameIndex == 9)
                            _enemy.TakeDamage(Damage, Color.Red);                    
                }
                    //Nguoc lai, muc tieu dang o ngoai tam danh
                else
                    //Neu dang o trang thai Attack thi chuyen ve Idle
                    if (_UnitState == UnitState.Attack)
                        Idle();
            }
                //Neu muc tieu da bi tieu diet, va ko co muc tieu moi, thi chuyen ve trang thai Idle
            else
            {
                if (_UnitState == UnitState.Attack)
                    Idle();
            }

            Textures[index].Update();
        }

        public override void Draw(GameTime gameTime)
        {
            Rectangle rect = GetNextRect();
            SpriteBatch _SpriteBatch = (SpriteBatch)Game.Services.GetService(typeof (SpriteBatch));
            int index = GetUnitStateIndex();
            Textures[index].Draw(_SpriteBatch, _position - new Vector2(15, 15), 
                rect, Color.White, 0, Vector2.Zero, 1.5f, SpriteEffects.None, 0);
            base.Draw(gameTime);
        }
    }
}
